import {defineConfig, loadEnv} from 'vite'
import createVitePlugins from './src/vite/plugins';
import path from 'path'


let base = '/'
if (process.env.VITE_APP_BUILD_MODE === 'crx') base = './'
if (process.env.VITE_APP_BUILD_MODE === 'cdn') base = 'https://cdn.kongfandong.cn/howdz/dist/'

// https://vitejs.dev/config/
export default defineConfig(({mode, command}) => {
    const env = loadEnv(mode, process.cwd());
    const {VITE_APP_ENV} = env;
    return {
        base: base,
        plugins: createVitePlugins(env, command === "build"),
        // 将 src 替换成 @
        resolve: {
            alias: {
                // 配置别名
                '@': path.resolve(__dirname, 'src'),
            },
        },
        server: {
            proxy: {
                // 带选项写法：http://localhost:5173/api/bar -> http://jsonplaceholder.typicode.com/bar
                '/api/baidu_sugrec': {
                    target: 'https://www.baidu.com/sugrec?&prod=pc&wd=',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api\/baidu_sugrec/, ''),
                },
                '/dev-api': {
                    target: 'http://localhost:8080/',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/dev-api/, '')
                }
            }
        },
        build: {
            // 输出目录
            outDir: process.env.VITE_APP_BUILD_MODE === 'crx' ? 'crx' : 'dist'
        },
        css: {
            // css预处理器
            preprocessorOptions: {
                scss: {
                    // 引入 mixin.scss 这样就可以在全局中使用 mixin.scss中预定义的变量了
                    // 给导入的路径最后加上 ;
                    additionalData: `
                        @use "src/assets/styles/element/index.scss" as *;
                        @import "@/assets/styles/variables.module.scss";
                    `
                }
            }
        },
    }
})
